package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Miyako
 * @Date: 2024-08-29-10:45
 * @Description:
 */
@Slf4j
@RestController
@Api(tags = "用户模块")
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    UserService userService;
    /**
     * @param: 接收Json字符串
     * @return: code：2000 + 查询到的用户信息
     * @description: 登录功能
     **/
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        UserVO userVO = userService.login(userLoginParam);
        log.debug(userVO+"");
        return JsonResult.ok(userVO);
    }

    /**
     * @param: 接收前端的字符串（前端QS框架实现，可以不用注解解析Json格式）
     * @return: 2000状态码
     * @description:后台保存用户信息
     **/
    @PostMapping("save")
    public JsonResult saveUser (UserSaveParam userSaveParam){

        log.debug("userSaveParam:"+userSaveParam);

        userService.saveUser(userSaveParam);

        return JsonResult.ok();
    }


    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("接收到数据+"+userQuery);

        List<UserQuery> userQueries = userService.selectUser(userQuery);

        log.debug("返回数据"+userQueries);

        return JsonResult.ok(userQueries);

    }
    
    /*
     * @param: [userId] 用户id
     * @return: 2000
     * @description:使用Restful风格传参url地址用{}占位，并在传参上添加@PathVarible注解
     **/
    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){

        userService.resetPassword(userId);

        return JsonResult.ok();
        
    }

    @PostMapping("/status/{userId}/{status}")
    public JsonResult changeStatus(@PathVariable Long userId , @PathVariable String status){

        userService.changeStatus(userId,status);

        return JsonResult.ok();

    }
    @PostMapping("/delete/{userId}")
    public  JsonResult deleteUser(@PathVariable Long userId){
        log.debug("接收到参数"+userId);

        userService.deleteUser(userId);

        log.debug("删除成功");
        return  JsonResult.ok();
    }

}
